/**
 * 防抖
 * @param func 
 * @param delay 
 * @returns 
 */
 export const debounce = (func:Function, delay:number) => {
    let timer:any;
    return function (...args:any[]) {
      if(timer) {
        clearTimeout(timer);
      }
      timer = setTimeout(() => {
        func.apply(this, args);
        clearTimeout(timer);
      }, delay);
    };
  };
  
  /**
   * 节流
   * @param {function} fun 
   * @param {number} time 
   * @returns 
   */
  export const throttle = (fun:Function,time:number) => {
    let t1 = 0; // 初始时间
    return function(){
        let t2 = Date.now(); // 当前时间
        if(t2-t1>time){
            fun.apply(this,arguments)
            t1=t2
        }
    }
  }

// 字符串相关的操作  
/**
* @func 格式化数字
* @params num:number  
* @return string
* 1218807  =>  121.9w
*/
export function formatTenThousand(num:number):string {
    const numStr = String(num);
    if(numStr.length<=3) {
        return numStr;
    }
    let wholeNumber = numStr.substring(0,numStr.length-3);
    // console.log(wholeNumber);
    const shousands = numStr.substring(numStr.length-3);
    // 通过首位(百位)与余位拼接得到的小数 toFixd(0) 四舍五入
    let decimalNumber = Number(shousands.substring(0,1)+"."+
        shousands.substring(1)).toFixed(0)
    // console.log(decimalNumber);
    // 百位 toFixed 进位后可能是10
    if (decimalNumber.length ==2){
        decimalNumber = "0";
        wholeNumber = String(Number(wholeNumber) + 1);
    }
    return `${wholeNumber}.${decimalNumber}k`
    }

  /**
* 将搜索记录存入LocalStorage
* @param historyList 
*/
export const setSearchHistory_loc = (historyList:string[]) => {
    window.localStorage.setItem("search_history",
      JSON.stringify(historyList));
    }
    /**
    * 从LocalStorage获取搜索记录
    * @returns searchHistory
    */
    export const getSearchHistory_loc = () => {
      return JSON.parse(window.localStorage.getItem('search_history')||'[]')
    }

  export const getName=(list: any[])=>{
    let str="";
    list.map((item,index)=>{
        str+=index===0?item.name:"/"+item.name;
        return item;
    })
    return str;
  }
  export const filterIndex = (rankList: string | any[]) => {
    for (let i = 0; i < rankList.length; i++) {
        if (rankList[i].tracks.length && rankList[i + 1].tracks.length) {
            return i + 1;
        }
    }
}
export const getdataCount = (createTime: number) => {
  let curTime = + new Date();
  let date = Math.floor((curTime - createTime)/1000/60/60/24)
  if (date > 30 && date < 365) {
      return `${Math.floor(date/30)}月前` 
  } else if (date >= 365) {
      return `${Math.floor(date/365)}年前`
  } else {
      return `${date}天前`
  }
}

export const gainImages = (imageList: any[]) => {
  let res: any[] = []
  imageList.forEach(item => {
      res.push(item.url)
  })
  return res
}


export const filterList = (params:any) => {
    const { inputContent, activeKey, city='', data } = params
    let result = data;
    if (city && city !== '所有城市'&& city !== '城市') {
        result = result.filter(content => content.pos.includes(city))
    }
    return result
}